import { utoa } from '../utils'

const MAIN_FILE_NAME = 'App.vue'

let testUrl = 'http://124.222.131.201:8070'
if (typeof window !== "undefined") {
  const { protocol, hostname } = window.location;
  testUrl = `${protocol}//${hostname}:8070`;
}

export const usePlayground = (source: string) => {
  const code = decodeURIComponent(source)
  const originCode = {
    [MAIN_FILE_NAME]: code,
  }

  const encoded = utoa(JSON.stringify(originCode))
  const isPreview = location.host.startsWith('preview')
  let link = `${testUrl}/#${encoded}`
  if (isPreview) {
    const pr = location.host.split('-', 2)[1]
    link = `${testUrl}/?pr=${pr}#${encoded}`
  }
  return {
    encoded,
    link,
  }
}
